﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using wms.Common;

namespace wms.Pages
{
    /// <summary>
    /// GoodsSelect.xaml 的交互逻辑
    /// </summary>
    public partial class GoodsSelect : Window
    {
        private string type;
        private int pageNo = 1;
        private int pagePer = 5000;
        private int pages = 1;
        private List<DataRow> _result;

        private const string defaultString = "输入物品编码、名称、类型、客户名称等关键字按回车键搜索";

        public GoodsSelect()
        {
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString());
            InitializeComponent();
          
        }
        public GoodsSelect(string type)
        {
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString());
            this.type = type;
            InitializeComponent();

        }

        private void txt_scan_GotFocus(object sender, RoutedEventArgs e)
        {
            if (this.txt_scan.Text == defaultString)
            {
                this.txt_scan.Text = "";
            }
            this.txt_scan.Foreground = Brushes.Black;
        }

        private void txt_scan_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.txt_scan.Text.Length == 0)
            {
                this.txt_scan.Text = defaultString;
                this.txt_scan.Foreground = Brushes.Gray;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txt_scan_LostFocus(sender, e);
        }

        private void txt_scan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                query();
            }
        }

        private void query()
        {
            Dictionary<string, object> param = new Dictionary<string, object>();
            string sql = "select id,code,name,type,size,value,client,dbo.f_goods_quantity(id) as quantity,weight from t_goods where name like '%' + @key + '%' or code like '%' + @key + '%' or type like '%' + @key + '%' or client like '%' + @key + '%'";
            string key = this.txt_scan.Text;
            param.Add("@key", key);

            DataTable dt = DBHelper.GetDataTable(sql, param, "id", pagePer, (pageNo - 1) * pagePer);
            dataGrid.ItemsSource = dt.DefaultView;
            int count = DBHelper.GetCount(sql, param);

            this.pages = (count + pagePer - 1) / pagePer;
            //this.lbl_page.Content = String.Format("共{0}条 每页{1}条，当前{2}/{3}", count, pagePer, pageNo, pages);
        }

        private void btn_ok_Click(object sender, RoutedEventArgs e)
        {
            IList list = this.dataGrid.SelectedItems;
            if (list != null && list.Count > 0)
            {
                this.Result = new List<DataRow>();
                foreach (var item in list)
                {
                    DataRow row = (item as DataRowView).Row;
                    var num = row["quantity"].ToString();
                    if (this.type=="出库")
                    {
                        if (Convert.ToInt32(num) <= 0)
                        {
                            MessageBox.Show("物品名称【" + row["name"].ToString() + "】,库存不足，不能出库!");
                            return;
                        }
                    }
                   this.Result.Add(row);
               }
                this.Close();
            }
            else
            {
                MessageBox.Show("没有选择任何数据");
            }
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        public List<DataRow> Result
        {
            get
            {
                return _result;
            }

            set
            {
                _result = value;
            }
        }

        private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            btn_ok_Click(sender, e);
        }
    }
}
